<?php
namespace App\Logics;

use App\Models\ChannelModel;


use App\Models\SystermAdModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


//逻辑处理
class SystermAdLogic extends BaseLogic {

    public  function index(array $para){
        $page = isset($para['page'])?(int)$para['page']:1;
        $per_page = isset($para['per_page'])?(int)$para['per_page']:20;
        $title = isset($para['title'])?$para['title']:'';
        $is_show_share = isset($para['is_show_share'])?(int)$para['is_show_share']:-1;
        $cha_id = isset($para['cha_id'])?(int)$para['cha_id']:-1;

        $where = $searchWhere = [];
        if($is_show_share==0 || $is_show_share==1){
            $where['is_show_share'] = $is_show_share;
            $searchWhere[] = ['is_show_share',$is_show_share ];
        }
        if($cha_id>0){
            $where['cha_id'] = $cha_id;
            $searchWhere[] = ['cha_id',$cha_id ];
        }
        if($title){
            $where['title'] = $title;
            $searchWhere[] = ['title', 'like', $title . '%'];
        }
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }

        $model = SystermAdModel::where($searchWhere)->orderBy('id','desc');
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $channel=ChannelLogic::pluck('cha_describe','cha_id');

        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['cha_name']=isset($channel[$v['cha_id']])?$channel[$v['cha_id']]:'';
            $data['data'][$k]['create_time']=$v['create_time']?date('Y-m-d H:i:s',$v['create_time']):'-';
        }

        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url,
            'channel'=>$channel
        ];
        return $arr;
    }

    /** 添加
     * @param array $para
     * @param Request $request
     * @return mixed
     */
    public  function add(array $para,Request $request){
        $title= isset($para['title'])?$para['title']:'';
        $content_url=isset($para['content_url'])?$para['content_url']:'';
        $is_show_share=isset($para['is_show_share'])?$para['is_show_share']:1;
        $is_show_share==1?1:0;
        $cha_id=isset($para['cha_id'])?$para['cha_id']:0;


        if(empty($title)){
            return error('请填写标题');
        }
        if(empty($content_url)){
            return error('请填写跳转地址');
        }
        if($cha_id<1){
            return error('请选择渠道');
        }
        $channel=ChannelModel::where('cha_id',$cha_id)->select('cha_name')->first();
        if(empty($channel)) return error('选择的渠道不存在');
        $channel=ob_to_array($channel);


        if(!$request->hasFile('litpic')){
            return error('请上传图片');
        }

        $litpic='';
        if($request->hasFile('litpic')){
            $data=$this->uploadImg($request,'litpic','lunbo');
            if($data['status']==0) return error($data['data']);
            $litpic=$data['data'];
        }
        $data=[
            'title'=>$title,
            'content_url'=>$content_url,
            'is_show_share'=>$is_show_share,
            'litpic'=>$litpic,
            'cha_id'=>$cha_id,
            'create_time'=>time(),
            'show_location'=>$channel['cha_name']
        ];

        $res=SystermAdModel::insert($data);
        if($res){
            return success('添加成功');
        }
        if($data['litpic']) $this->deleteImg($data['litpic']);
        return error('添加失败');
    }

    /** 编辑
     * @param array $para
     * @param Request $request
     * @return mixed
     */
    public  function edit(array $para,Request $request){
        $title= isset($para['title'])?$para['title']:'';
        $content_url=isset($para['content_url'])?$para['content_url']:'';
        $is_show_share=isset($para['is_show_share'])?$para['is_show_share']:1;
        $is_show_share==1?1:0;
        $cha_id=isset($para['cha_id'])?$para['cha_id']:0;
        $id=isset($para['id'])?(int)$para['id']:0;

        if($id<1){
            return error('缺少参数');
        }
        if(empty($title)){
            return error('请填写标题');
        }
        if(empty($content_url)){
            return error('请填写跳转地址');
        }
        if($cha_id<1){
            return error('请选择渠道');
        }
        $channel=ChannelModel::where('cha_id',$cha_id)->select('cha_name')->first();
        if(empty($channel)) return error('选择的渠道不存在');
        $channel=ob_to_array($channel);
        $ad=SystermAdModel::where('id',$id)->first();
        $ad=ob_to_array($ad);
        if(empty($ad)){
            return error('参数有误');
        }
        if(empty($ad['litpic']) && !$request->hasFile('litpic')){
            return error('请上传图片');
        }

        $litpic=$ad['litpic'];
        $newImg='';
        if($request->hasFile('litpic')){
            $data=$this->uploadImg($request,'litpic','lunbo');
            if($data['status']==0) return error($data['data']);
            $litpic=$data['data'];
            $newImg=$data['data'];
        }
        $data=[
            'title'=>$title,
            'content_url'=>$content_url,
            'is_show_share'=>$is_show_share,
            'litpic'=>$litpic,
            'cha_id'=>$cha_id,
            'create_time'=>time(),
            'show_location'=>$channel['cha_name']
        ];
        $res=SystermAdModel::where('id',$id)->update($data);
        if($res){
            if($ad['litpic'] && $newImg) $this->deleteImg($ad['litpic']);
            return success('修改成功');
        }
        if($newImg) $this->deleteImg($newImg);
        return error('修改失败');
    }

    /**
     *  删除数据
     * @param int $id
     */
    public function delete(int $id){
        $res=SystermAdModel::where('id',$id)->delete();
        if($res){
            return success('删除成功');
        }
        return error('删除失败');
    }

    /**
     * @param int $id
     * @return mixed
     */
    public function ajaxEdit(int $id){
        $ad=SystermAdModel::where('id',$id)->select('is_show_share')->first();
        $ad=ob_to_array($ad);
        if(empty($ad)){
            return error('修改失败');
        }
        $zhi=$ad['is_show_share']==0?1:0;
        $res=SystermAdModel::where('id',$id)->update(['is_show_share'=>$zhi]);
        if($res){
            return success('ok',['zhi'=>$zhi]);
        }
        return error('修改失败');
    }

    /** 获取单条数据
     * @param int $id
     * @param array $fields
     * @return mixed
     */
    public function  getOne(int $id,array $fields=['*']){
        $data=SystermAdModel::where('id',$id)->select($fields)->first();
        return ob_to_array($data);
    }




















}